package com.chiancloud.shield.server.services.curator;

import org.apache.curator.x.discovery.ServiceInstance;
import org.apache.curator.x.discovery.ServiceType;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonTypeInfo;

/**
 * Created by wanghui on 14-8-20.
 */

@JsonIgnoreProperties(ignoreUnknown = true)
public class CuratorServiceInstance<T> {

    public String name;
    public String id;
    public String address;
    public Integer port;
    public Integer sslPort;
    public T payload;
    public long registrationTimeUTC;
    public ServiceType serviceType;
    public CuratorUriSpec uriSpec;

    public CuratorServiceInstance() {
    }

    public CuratorServiceInstance(ServiceInstance<T> si) {
        this.name = si.getName();
        this.id = si.getId();
        this.address = si.getAddress();
        this.port = si.getPort();
        this.sslPort = si.getSslPort();
        this.payload = si.getPayload();
        this.registrationTimeUTC = si.getRegistrationTimeUTC();
        this.serviceType = si.getServiceType();
        this.uriSpec = new CuratorUriSpec();
        this.uriSpec.addAll(si.getUriSpec().getParts());
    }


    @Override
    public String toString() {
        final StringBuilder sb =
                new StringBuilder("CuratorServiceInstance{");
        sb.append("name='").append(name).append('\'');
        sb.append(", id='").append(id).append('\'');
        sb.append(", address='").append(address).append('\'');
        sb.append(", port=").append(port);
        sb.append(", sslPort=").append(sslPort);
        sb.append(", payload=").append(payload);
        sb.append(", registrationTimeUTC=").append(registrationTimeUTC);
        sb.append(", serviceType=").append(serviceType);
        sb.append(", uriSpec=").append(uriSpec);
        sb.append('}');
        return sb.toString();
    }

    @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
    public T getPayload() {
        return payload;
    }
}
